package oop.ex2.main;

import oop.ex2.tools.*;

/**
 * 
 * @author yossavi
 *
 */
public class Variable{
	
	private String type, name;
	private boolean isFinal, value=false;
	
	/**
	 * Constructor for parameters
	 * @param isFinal- is the variable final
	 * @param type- the type of the variable
	 * @param name- the name of the variable
	 */
	public Variable (boolean isFinal, String type,  String name) {
		this.isFinal=isFinal;
		this.type=type;
		this.name=name;
		this.value=true;
	}
	
	/**
	 * Constructor for variables
	 * checks if the value assign right otherwise throw compilationException
	 * @param isFinal- is the variable final
	 * @param type- the type of the variable
	 * @param name- the name of the variable
	 * @param value- is it got value
	 * @throws compilationException 
	 */
	public Variable (boolean isFinal, String type,  String name, String value) throws compilationException   {
		this.isFinal=isFinal;
		this.type=type;
		this.name=name;
		if (value!=null) {
			setValue(value);
		}
		else if (isFinal)
			throw new FinalException("Final must has value");
	    
	}
	
	/**
	 * return the variable name
	 * @return the variable name
	 */
	public String getName () {
		return this.name;
	}
	
	/**
	 * return the variable type
	 * @return the variable type
	 */
	public String getType () {
		return this.type;
	}
	
	/**
	 * return if the variable is final
	 * @return true if the variable is final, else otherwise
	 */
	public boolean getIsFinal () {
		return this.isFinal;
	}
	
	/**
	 * return if the variable got value
	 * @return true if the variable got value, else otherwise
	 */
	public boolean getValue () {
		return this.value;
	}
	
	/**
	 * sets the value as true if it match is type
	 * @param value the value for check
	 * @throws compilationException
	 */
	public void setValue (String value) throws compilationException {
		if (isFinal && this.value==true)
			throw new FinalException("Final can't be modified");
		this.value=true;
		if (!CheckType.isValueMuchType (value, this.type))
			throw new MismatchTypeException("Wrong type assigning");
		
	}
	
	
}
